package org.o2opuzi.ms.lib;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.o2opuzi.lib.http.Http;

public class TPUtils {
	
	
	private Map<String,Object> ipmaps = new ConcurrentHashMap<String,Object>();
	
	private int count = 0;
			
	public void run(final String ip,final int port){
		
		try {
			Http http = new Http("http://tpv2.scol.com.cn/netvote/16sclytop/safecodeusing");
			RequestConfig config = RequestConfig.custom().setProxy(new HttpHost(ip, port)).setAuthenticationEnabled(true).build();
			http.setConfig(config);
			http.addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
			http.addHeader("X-Requested-With", "XMLHttpRequest");
			http.addHeader("Host", "tpv2.scol.com.cn");
			http.addHeader("Referer", "http://tpv2.scol.com.cn/16sclytop/");
			http.addHeader("Origin", "http://tpv2.scol.com.cn");
			http.addHeader("Cookie", "Hm_lvt_0f827d2d0df4dc71c86b6160f1a1970c=1483675024; Hm_lpvt_0f827d2d0df4dc71c86b6160f1a1970c=1483675024");
			http.doPost().toString();
			Thread.sleep(1000);
			Http http2 = new Http("http://tpv2.scol.com.cn/netvote/16sclytop");
			RequestConfig config2 = RequestConfig.custom().setProxy(new HttpHost(ip, port)).setAuthenticationEnabled(true).build();
			http2.setConfig(config2);
			http2.addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
			http2.addHeader("X-Requested-With", "XMLHttpRequest");
			http2.addHeader("Host", "tpv2.scol.com.cn");
			http2.addHeader("Referer", "http://tpv2.scol.com.cn/16sclytop/");
			http2.addHeader("Origin", "http://tpv2.scol.com.cn");
			http2.addHeader("Cookie", "Hm_lvt_0f827d2d0df4dc71c86b6160f1a1970c=1483675024; Hm_lpvt_0f827d2d0df4dc71c86b6160f1a1970c=1483675024");
			http2.addParam("myitem", "33902");
			String ret = http2.doPost().toString();
			if(ret.indexOf("推荐成功") == -1){
				System.out.println("投票失败：："+ret);
				System.out.println("剩余IP::"+ipmaps.size());
				ipmaps.remove(ip);
				System.out.println("剩余IP::"+ipmaps.size());
			}else{
				count++;
				System.out.println(ret);
				System.out.println("已投::"+count+" 票");
			}
		
		} catch (Exception e) {
			System.out.println("投票失败：："+e.getMessage());
			System.out.println("剩余IP::"+ipmaps.size());
			ipmaps.remove(ip);
			System.out.println("剩余IP::"+ipmaps.size());
		}
	}
	
	public void initIPArray(){
		ipmaps.clear();
		Http http = new Http("http://api.ip.data5u.com/api/get.shtml?order=610ad77d2fe41ef931c1f4be1e5b5217&num=100&area=%E4%B8%AD%E5%9B%BD&carrier=0&protocol=0&an1=1&an2=2&an3=3&sp1=1&sp2=2&sp3=3&sort=1&system=1&distinct=0&rettype=0&seprator=%0D%0A");
		JSONArray array = http.doGet().toJsonObject().getJSONArray("data");
		for (int i = 0; i < array.size(); i++) {
			final JSONObject object = array.getJSONObject(i);
			ipmaps.put(object.getString("ip"),object);
		}
	}
	
	public void start(){
		final TPUtils client = this;
		while(true){
			if(ipmaps.size() <= 0){
				initIPArray();
			}
			new Thread(new Runnable() {
				@Override
				public void run() {
					for (Entry<String, Object> en : ipmaps.entrySet()) {
						final JSONObject object = (JSONObject) en.getValue();
						new Thread(new TPThread(ipmaps, object.getString("ip"), object.getIntValue("port"), client)).start();
					}
				}
			}).start();
			try {
				Thread.sleep(20000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	

	
}
class TPThread implements Runnable{

	private Map<String, Object> ipmaps;
	
	private String ip;
	
	private int port;
	
	private TPUtils client;
	
	@Override
	public void run() {
		try {
			System.out.println("剩余IP::"+ipmaps.size());
			if(ipmaps.size() <= 3){
				client.initIPArray();
			}
			client.run(ip,port);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TPThread(Map<String, Object> ipmaps, String ip, int port,
			TPUtils client) {
		super();
		this.ipmaps = ipmaps;
		this.ip = ip;
		this.port = port;
		this.client = client;
	}
	
	
	
}
